package com.gaolang.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaolang.web.entities.LoginLog;
import com.gaolang.web.response.Result;
import com.gaolang.web.service.ILoginLogService;
import com.gaolang.web.vo.LoginLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 登录日志表 前端控制器
 * </p>
 *
 * @author gaolang
 * @since 2021-03-03
 */
@RestController
@RequestMapping("/login-log")
@Api(value = "登录日志",tags = "登录日志接口")
public class LoginLogController {

    @Autowired
    ILoginLogService loginLogService;

    @ApiOperation(value = "插入登入日志",notes = "登录日志")
    @PostMapping("/insert")
    public Result insert(HttpServletRequest request, Principal principal){
        LoginLog loginLog = loginLogService.createLoginLog(request, principal);
        loginLogService.save(loginLog);
        return Result.ok();
    }

    @ApiOperation(value = "登录日志列表",notes = "登录日志--分页")
    @PostMapping("/getList")
    public Result list(@RequestParam(required = true,defaultValue = "1")Integer current
            , @RequestParam(required = true,defaultValue = "6")Integer size,@RequestBody LoginLogVO loginLogVO){
        Page<LoginLog> page = new Page<>(current,size);
        Page<LoginLog> pages = new Page<>();
        if (Objects.isNull(loginLogVO)) {
            pages = loginLogService.page(page);
        }else {
            QueryWrapper<LoginLog> wrapper = new QueryWrapper<>();
            if (!Objects.isNull(loginLogVO.getUsername()) && !"".equals(loginLogVO.getUsername().trim())) {
                wrapper.eq("username",loginLogVO.getUsername());
            }

            if (!Objects.isNull(loginLogVO.getIp()) && !"".equals(loginLogVO.getIp().trim())) {
                wrapper.eq("ip",loginLogVO.getIp());
            }

            if (!Objects.isNull(loginLogVO.getLocation()) && !"".equals(loginLogVO.getLocation().trim())) {
                wrapper.eq("location",loginLogVO.getLocation());
            }

            pages = loginLogService.page(page,wrapper);
        }

        return Result.ok().data("total",pages.getTotal()).data("records",pages.getRecords());
    }

    @ApiOperation(value = "删除登录日志",notes = "更具ID删除")
    @PostMapping("/deleteById")
    public Result delete(Long id){
        loginLogService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "批量删除",notes = "批量删除登录日志")
    @PostMapping("/batchDelete")
    public Result batchDelete(String ids){
        String[] idList = ids.split(",");
        List<Long> list = new ArrayList<>();
        if (idList.length > 0) {
            for (String s : idList) {
                list.add(Long.parseLong(s));
            }
        }
        loginLogService.removeByIds(list);
        return Result.ok();
    }
}
